package com.gxa.controller.SystemController;


import com.gxa.entity.Admin;
import com.gxa.entity.JwtToken;
import com.gxa.entity.Role;
import com.gxa.oauth2.OAuth2Token;
import com.gxa.service.system.AdminService;
import com.gxa.utils.JwtUtil;
import com.gxa.utils.R;
import com.gxa.vo.AdminVo;
import com.gxa.vo.ListVo;
import com.gxa.vo.MenuVo;
import com.gxa.vo.RoleVo;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(tags = "管理员管理模块")
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation(value="登录接口",notes="输入的用户信息",produces = "application/json",response = R.class)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回登录是否成功",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @PostMapping("/login")
    public R login(@ApiParam(name="Admin-为参数名",value = "管理员名参数--这里输入要登录的管理员信息",required = true)@RequestBody AdminVo admin){
        System.out.println(admin);
        System.out.println("name:--->" + admin.getAccount());
        System.out.println("pwd:--->" + admin.getPwd());

        if(admin.getAccount()!=null&&admin.getPwd()!=null) {
            Integer id = adminService.queryAdmin(admin);
            if(id!=null){
                String token = JwtUtil.sign(admin.getAccount(), admin.getPwd());
                Subject subject = SecurityUtils.getSubject();//拿到subject
                subject.login(new OAuth2Token(token));
                AdminVo adminVo = adminService.queryAdminById(id);
                System.out.println(token);
                //根据用户id返回List<MenuVo>(菜单列表)  将父级菜单放入ListVo的menuVo中，子级菜单放入menuVo中
//                List<MenuVo> menuVos = adminService.queryMenus(id);
                //查找父级菜单
                List<MenuVo> menuVos = adminService.selPMenu(id);
                List<ListVo> listVos = new ArrayList<>();
                for(MenuVo menuVo : menuVos){
                    List<MenuVo> menuVos1 = adminService.selSMenu(menuVo.getId());
                    if(menuVos1!=null) {
                        ListVo listVo = new ListVo();
                        listVo.setMenuVo(menuVo);
                        listVo.setMenuVoList(menuVos1);
                        listVos.add(listVo);
                    }
                }
                System.out.println(listVos);
                return R.ok("登录成功").put("data",listVos).put("Authorization",token).put("user",adminVo);
            }
        }
        return R.error("登录失败");
    }



    @ApiOperation(value="注册接口",notes="输入的用户信息",produces = "application/json",response = R.class)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回注册是否成功",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "注册已超时，请重新注册"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @PostMapping("/regist")
    public R regist(@ApiParam(name="Admin-为参数名",value = "管理员名参数--这里输入要登录的管理员信息",required = true)@RequestBody AdminVo admin){
        System.out.println(admin);
        System.out.println("name:--->" + admin.getName());
        System.out.println("pwd:--->" + admin.getPwd());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        admin.setTime(sdf.format(new Date()));
        admin.setType(0);
        admin.setStatus(1);
        adminService.addUser(admin);
        return R.ok("注册成功");
    }




    //    @ApiOperation(value="Admin列表接口",notes="查询全部管理员信息",produces = "application/json",response = R.class)
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
//    })
//    @ApiResponses(value = {
//            @ApiResponse(code = 200, message = "返回所有的管理员详细信息",examples = @io.swagger.annotations.Example(
//                    value = {
//                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
//                    })),
//            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
//            @ApiResponse(code = 307, message = "令牌格式不正确"),
//            @ApiResponse(code = 401, message = "没有权限"),
//            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
//            @ApiResponse(code = 500, message = "服务器内部错误")
//    })
    @RequiresPermissions("admin:sel")
    @GetMapping("/admin/queryAllAdmin")
    public R queryAllAdmin(){
        List<AdminVo> adminVos = adminService.queryAllAdmin();
        System.out.println(adminVos);
        return R.ok().put("data",adminVos);

    }

    @ApiOperation(value="Admin查询接口",notes="根据输入内容查询管理员信息",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回所有的管理员详细信息",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("admin:sel")
    @PostMapping("/admin/qureyAdmins")
    public R queryAdmins(@ApiParam(name="admin",value = "管理员名参数--这里输入要查询的管理员名",required = true)@RequestBody AdminVo adminVo){
        System.out.println(adminVo.getName());
        List<AdminVo> adminVos = adminService.queryAdminsByName("%"+adminVo.getName()+"%");
        return R.ok().put("data",adminVos);
    }


    @ApiOperation(value="进入Admin增加接口",notes="点击增加管理员进入接口",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回角色列表",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("admin:sel")
    @GetMapping("/admin/preAddAdmin")
    public R preAddAdmin(){
        List<RoleVo> roleVos = adminService.queryAllRole();
        return R.ok().put("data",roleVos);
    }

    @ApiOperation(value="Admin增加接口",notes="增加管理员接口",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回增加是否成功",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("admin:add")
    @PostMapping("/admin/addAdmin")
    public R addAdmin(@ApiParam(name="admin",value = "管理员参数--这里写输入的管理员信息描述",required = true)@RequestBody AdminVo adminVo){
        System.out.println(adminVo);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        adminVo.setTime(sdf.format(new Date()));
        adminVo.setType(1);
        adminService.addAdmin(adminVo);
        return R.ok();
    }

    @ApiOperation(value="进入编辑Admin接口",notes="进入操作列编辑管理员操作接口",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "根据管理员id查询到的管理员详细信息",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("admin:sel")
    @PostMapping("/admin/preEditAdmin")
    public R preEditAdmin(@ApiParam(name="admin",value = "管理员id",required = true)@RequestBody AdminVo admin){
        System.out.println(admin.getId());
        AdminVo adminVo = adminService.queryAdminById(admin.getId());
        List<RoleVo> roleVos = adminService.queryAllRole();
        System.out.println(roleVos);
        Map map = new HashMap<>();
        map.put("admin",adminVo);
        map.put("roles",roleVos);
        return R.ok().put("data",map);
    }

    @ApiOperation(value="编辑Admin接口",notes="编辑管理员操作接口",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回编辑是否成功",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("admin:update")
    @PostMapping("/admin/editAdmin")
    public R editAdmin(@ApiParam(name="admin",value = "管理员参数--这里写输入的管理员信息描述",required = true)@RequestBody AdminVo admin){
        System.out.println(admin);
        adminService.updateAdmin(admin);
        return R.ok();
    }

    @ApiOperation(value="删除Admin接口",notes="删除管理员操作接口",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回删除是否成功",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("admin:del")
    @DeleteMapping("/admin/delAdmin")
    public R delAdmin(@ApiParam(name="admin",value = "管理员id",required = true)@RequestBody AdminVo admin){
        System.out.println(admin.getId());
        if (adminService.delAdmin(admin.getId())){
            return R.ok("删除成功");
        }
        return R.ok("不可删除该管理员");
    }

    @ApiOperation(value="修改Admin状态接口",notes="修改管理员状态操作接口",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回修改是否成功",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("admin:update")
    @PutMapping("/admin/editAdminStatus")
    public R editAdminStatus(@ApiParam(name="admin",value = "只传当前管理员的id，和改变后的状态",required = true)@RequestBody AdminVo adminVo){
        System.out.println(adminVo);
        adminService.updateAdminStatus(adminVo.getId(),adminVo.getStatus());
        return R.ok();
    }


    @ApiOperation(value="修改Admin密码接口",notes="修改管理员密码操作接口",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回修改是否成功",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("admin:update")
    @PutMapping("/admin/editAdminPwd")
    public R editAdminPwd(@ApiParam(name="admin",value = "只传当前管理员的id，和改变后的密码pwd",required = true)@RequestBody AdminVo adminVo){
        System.out.println(adminVo);
        adminService.updateAdminPwd(adminVo.getId(),adminVo.getPwd());
        return R.ok("修改成功");
    }
}
